<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="需求ID" prop="requireId">
        <el-input
          v-model="queryParams.requireId"
          placeholder="请输入需求ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供方用户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入方案提供方用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供方公司" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入方案提供方公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="summaryList" @selection-change="handleSelectionChange">
      <el-table-column label="商机" align="center" prop="requireTitle" />
      <el-table-column label="供方用户" align="center" prop="userName" />
      <el-table-column label="推荐标签" align="center" prop="tag" />
      <el-table-column label="方案主图" align="center" prop="mainImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.mainImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="方案内容" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="预估价格" align="center" prop="prePrice" />
      <el-table-column label="供方公司" align="center" prop="companyName" />
      <el-table-column label="方案标签" align="center" prop="tags" :show-overflow-tooltip="true"/>
      <el-table-column label="方案状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ai_scheme_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:summary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:summary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求总结方案信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="需求标题" prop="requireId">
          <el-input v-model="form.requireTitle" placeholder="请输入需求ID" readonly/>
        </el-form-item>
        <el-form-item label="供方用户" prop="userId">
          <el-input v-model="form.userName" placeholder="请输入方案提供方用户" readonly/>
        </el-form-item>
        <el-form-item label="推荐标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入推荐标签" />
        </el-form-item>
        <el-form-item label="方案主图" prop="mainImage">
          <image-upload v-model="form.mainImage"/>
        </el-form-item>
        <el-form-item label="方案内容" prop="content">
          <!--<editor v-model="form.content" :min-height="192"/>-->
          <mavon-editor ref="md" prop="content" v-model="form.content" :subfield = "false"/>
        </el-form-item>
        <el-form-item label="方案标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入方案标签" />
        </el-form-item>
        <el-form-item label="方案状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择方案状态" clearable>
            <el-option
              v-for="dict in dict.type.ai_scheme_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="预估价格" prop="prePrice">
          <el-input v-model="form.prePrice" placeholder="请输入预估价格" />
        </el-form-item>
        <el-form-item label="供方公司" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入方案提供方公司名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSummary, getSummary, delSummary, addSummary, updateSummary } from "@/api/system/summary";
import VueMarkdown from 'vue-markdown';

export default {
  name: "Summary",
  dicts: ['ai_scheme_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求总结方案信息表格数据
      summaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requireId: null,
        userId: null,
        tag: null,
        mainImage: null,
        content: null,
        prePrice: null,
        companyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        requireId: [
            { required: true, message: "需求不能为空", trigger: "blur" }
        ],
        userId: [
            { required: true, message: "供方用户不能为空", trigger: "change" }
        ],
        tag: [
            { required: true, message: "推荐标签不能为空", trigger: "blur" }
        ],
        mainImage: [
            { required: true, message: "方案主图不能为空", trigger: "change" }
        ],
        status: [
            { required: true, message: "方案状态不能为空", trigger: "change" }
        ],
        content: [
            { required: true, message: "方案内容不能为空", trigger: "blur" }
        ],
        prePrice: [
            { required: true, message: "预估价格不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询需求总结方案信息列表 */
    getList() {
      this.loading = true;
      listSummary(this.queryParams).then(response => {
        this.summaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        requireId: null,
        userId: null,
        tag: null,
        mainImage: null,
        content: null,
        prePrice: null,
        companyName: null,
        tags: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作方案信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSummary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合作方案信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSummary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSummary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delSummary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/summary/export', {
        ...this.queryParams
      }, `summary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
